package data_structure;

public class leetcode_0110 {
    public boolean isBalanced(TreeNode root) {
        boolean[] flag=new boolean[1];
        if(root==null) return true;
        flag[0]=true;
        isbalance(root,flag);
        return flag[0];
    }
    public int isbalance(TreeNode node,boolean[] flag){
        if(node==null) return 0;
        if(node.left==null&&node.right==null) return 1;
        int left=isbalance(node.left,flag);
        int right=isbalance(node.right,flag);
        if(left-right<-1||left-right>1) flag[0]=false;
        return Math.max(left,right)+1;
    }
}
